home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVLN03.C < prev    next >
C/C++ Source or Header  |  1993-03-27  |  7KB  |  225 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /*
  20. gdevln03.c
  21. Ghostscript driver for DEC LN03 printer
  22.  
  23. Ulrich Mueller, Div. PPE, CERN, CH-1211 Geneva 23 <ulm@vsnhd1.cern.ch>
  24. This code is subject to the GNU General Public License
  25.  
  26. ulm 91-02-13 created as driver for gs 2.1.1
  27. ulm 91-07-23 adapted to gs 2.2
  28. ulm 91-08-21 changed memory allocation to gs_malloc,
  29.          ported to VMS (contributed by Martin Stiftinger, TU Vienna)
  30. lpd 91-11-24 sped up by removing multiplies from inner loop
  31. ijmp 92-04-14 add support for la75/la50 (macphed@dvinci.usask.ca)
  32. ulm 92-09-25 support letter size paper (8.5" x 11")
  33. */
  34.  
  35. #include "gdevprn.h"
  36.  
  37. /* Forward references */
  38. private int sixel_print_page(P3(gx_device_printer *pdev,
  39.                    FILE *prn_stream, const char *init));
  40.  
  41. /* The device descriptor */
  42. private dev_proc_output_page(sixel_output_page);
  43. private dev_proc_print_page(ln03_print_page);
  44. /* We have to supply our own procs, since we have to intercept */
  45. /* output_page so we can open the printer in text mode. */
  46. private gx_device_procs sixel_procs =
  47.   prn_procs(gdev_prn_open, sixel_output_page, gdev_prn_close);
  48.  
  49. #ifdef A4
  50. #  define BOTTOM_MARGIN 0.5
  51. #else
  52. #  define BOTTOM_MARGIN 0.4
  53. #endif
  54. gx_device_printer gs_ln03_device =
  55.     prn_device(sixel_procs, "ln03",
  56.            DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  57.            300, 300,        /* x_dpi, y_dpi */
  58.            0, BOTTOM_MARGIN, 0, 0,    /* left, bottom, right, top margin */
  59.            1, ln03_print_page);
  60.  
  61. /*
  62.  * Initialization string: switch to graphics mode, 300 dpi
  63.  * <ESC>[!p        DECSTR    soft terminal reset
  64.  * <ESC>[11h        PUM        select unit of measurement
  65.  * <ESC>[7 I        SSU        select pixel as size unit
  66.  * <ESC>[?52h        DECOPM    origin is upper-left corner
  67.  * <ESC>[0t        DECSLPP    set maximum form length
  68.  * <ESC>[1;2475s    DECSLRM    set left and right margins
  69.  * <ESC>P0;0;1q            select sixel graphics mode
  70.  * "1;1            DECGRA    aspect ratio (1:1)
  71.  */
  72.  
  73. #define LN03_INIT \
  74.  "\033[!p\033[11h\033[7 I\033[?52h\033[0t\033[1;2475s\033P0;0;1q\"1;1"
  75.  
  76. private int
  77. ln03_print_page(gx_device_printer *pdev, FILE *prn_stream)
  78. {
  79.     return (sixel_print_page(pdev,prn_stream,LN03_INIT));
  80. }
  81.  
  82. /*
  83.  * LA75 dot matrix printer device.
  84.  * This uses North American 8.5 x 11 inch paper size.
  85.  */
  86. private dev_proc_print_page(la75_print_page);
  87. gx_device_printer gs_la75_device =
  88.     prn_device(sixel_procs, "la75",
  89.            85,
  90.            110,
  91.            144, 72,
  92.            0, 0, 0.5, 0,
  93.            1, la75_print_page);
  94.  
  95. #define LA75_INIT "\033P0;0;0q"
  96.  
  97. private int
  98. la75_print_page(gx_device_printer *pdev, FILE *prn_stream)
  99. {
  100.     return (sixel_print_page(pdev,prn_stream,LA75_INIT));
  101. }
  102.  
  103. /*
  104.  * LA50 dot matrix printer device.
  105.  * This uses North American 8.5 x 11 inch paper size.
  106.  */
  107. private dev_proc_print_page(la50_print_page);
  108. gx_device_printer gs_la50_device =
  109.     prn_device(sixel_procs, "la50",
  110.            85,
  111.            110,
  112.            144, 72,
  113.            0, 0, 0.5, 0,
  114.            1, la50_print_page);
  115. /* LA50's use a very primitive form of initialization */
  116.  
  117. #define LA50_INIT "\033Pq"
  118.  
  119. private int
  120. la50_print_page(gx_device_printer *pdev, FILE *prn_stream)
  121. {
  122.     return (sixel_print_page(pdev,prn_stream,LA50_INIT));
  123. }
  124.  
  125. /* ------ Internal routines ------ */
  126.  
  127. /* Open the printer in text mode before gdev_prn_output_page */
  128. /* opens it in binary mode. */
  129. private int
  130. sixel_output_page(gx_device *pdev, int num_copies, int flush)
  131. {    int code = gdev_prn_open_printer(pdev, 0);
  132.     if ( code < 0 )
  133.         return code;
  134.     return gdev_prn_output_page(pdev, num_copies, flush);
  135. }
  136.  
  137. /* Send the page to the printer. */
  138. private int
  139. sixel_print_page(gx_device_printer *pdev, FILE *prn_stream, const char *init)
  140. {
  141.     byte *in, *inp;
  142.     int lnum, lcount, l, count, empty, mask, c, oldc, line_size, in_size;
  143.  
  144.     line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  145.     in_size = line_size * 6;
  146.     in = (byte *)gs_malloc(in_size, 1, "sixel_print_page");
  147.  
  148.     /* Check allocation */
  149.     if (!in) return(-1);
  150.  
  151.     fputs(init,prn_stream);
  152.  
  153.     /* Print lines of graphics */
  154.     for (lnum = lcount = 0; lnum < pdev->height; lnum+=6, lcount++) {
  155.     gdev_prn_copy_scan_lines(pdev, lnum, inp = in, line_size * 6);
  156.  
  157.     mask = 0200;
  158.     oldc = 077;
  159.     empty = 1;
  160.  
  161.     for (l = pdev->width, count = 0; --l >= 0; count++) {
  162.         /* transpose 6*8 rectangle */
  163.         register byte *iptr = inp;
  164.         c = 077;
  165.         if (*iptr & mask)
  166.         c += 1;
  167.         if (*(iptr += line_size) & mask)
  168.         c += 2;
  169.         if (*(iptr += line_size) & mask)
  170.         c += 4;
  171.         if (*(iptr += line_size) & mask)
  172.         c += 010;
  173.         if (*(iptr += line_size) & mask)
  174.         c += 020;
  175.         if (*(iptr += line_size) & mask)
  176.         c += 040;
  177.         if (!(mask >>= 1)) {
  178.         mask = 0200;
  179.         inp++;
  180.         }
  181.  
  182.         if (c != oldc) {
  183.         if (empty) {
  184.             while (--lcount >= 0) {
  185.             /*
  186.              * Terminate record for VMS STREAM-LF file.
  187.              * This LF is ignored by the LN03.
  188.              */
  189.             fputc('\n', prn_stream);
  190.             /* Terminate previous line. */
  191.             fputc('-', prn_stream);
  192.             }
  193.             empty = lcount = 0;
  194.         }
  195.         if (count > 3)
  196.             /* use run length encoding */
  197.             fprintf(prn_stream, "!%d%c", count, oldc);
  198.         else
  199.             while (--count >= 0)
  200.             fputc(oldc, prn_stream);
  201.         oldc = c;
  202.         count = 0;
  203.         }
  204.     }
  205.     if (c != 077) {
  206.         if (count > 3)
  207.         /* use run length encoding */
  208.         fprintf(prn_stream, "!%d%c", count, c);
  209.         else
  210.         while (--count >= 0)
  211.             fputc(c, prn_stream);
  212.     }
  213.     }
  214.  
  215.     /* leave sixel graphics mode, eject page
  216.        <ESC>\        ST    string terminator
  217.        <FF>        FF    form feed */
  218.     fputs("\033\\\f", prn_stream);
  219.     fflush(prn_stream);
  220.  
  221.     gs_free((char *)in, in_size, 1, "sixel_print_page");
  222.  
  223.     return(0);
  224. }
  225.